/*
 * @Author: 黄宇/Hyuishine
 * @Date: 2021-12-06 14:07:32
 * @LastEditors: 黄宇/Hyuishine
 * @LastEditTime: 2022-05-06 10:00:17
 * @Description:
 * @Email: hyuishine@gmail.com
 * @Company: 3xData
 * @youWant: add you want
 */
import {
  app_update,
  app_info,
  area_list,
  get_dict,
  modifyPwd,
  getFaciliListApi,
  getFormDetailApi,
  getConclusionDetailApi,
  getProjectDetailApi,
  putAuditProcessApi,
} from "@/api/common.js";

const common = {
  state: {
    area: [],
    toastConfig: {},
    defaultConfig: {
      //默认配置
      show: false,
      text: "提示",
      loadingText: "加载中...",
      type: "default",
      timeOut: 1500,
      success: null,
      error: null,
    },
  },
  mutations: {
    save_area(state, data) {
      state.area = data;
    },
    //tf作为主动控制的参数
    switch_toast(state, config) {
      state.toastConfig = Object.assign({}, state.defaultConfig, config);

      if (state.toastConfig.type == "default") {
        setTimeout(() => {
          // 要是再次点击的是modal  就不关闭弹出的框
          if (
            state.toastConfig.type != "modal" &&
            state.toastConfig.type != "update"
          ) {
            if (state.toastConfig.success) {
              state.toastConfig.success();
            }

            state.toastConfig.show = false;
          }
        }, state.toastConfig.timeOut);
      } else if (
        state.toastConfig.type == "update" ||
        state.toastConfig.type == "modal"
      ) {
        state.toastConfig.error = function () {
          console.log(state.toastConfig.type);
          state.toastConfig.show = false;
        };
      } else {
        if (state.toastConfig.success) {
          setTimeout(() => {
            state.toastConfig.success();
            state.toastConfig.show = false;
          }, state.toastConfig.timeOut);
        }
        if (state.toastConfig.error) {
          setTimeout(() => {
            state.toastConfig.error();
          }, state.toastConfig.timeOut);
        }
      }
    },
  },
  actions: {
    app_update({ commit }, obj) {
      return new Promise((resolve, reject) => {
        app_update(obj, function (data) {
          resolve(data);
        });
      });
    },
    app_info({ commit }, obj) {
      return new Promise((resolve, reject) => {
        app_info(obj, function (data) {
          resolve(data);
        });
      });
    },

    area_list({ commit }, obj) {
      return new Promise((resolve, reject) => {
        area_list(obj, function (data) {
          const code = data.code;
          if (code == "200") {
            commit("save_area", data.data.rows);
            resolve(data);
          }
        });
      });
    },

    get_dict({ commit }, obj) {
      return new Promise((resolve, reject) => {
        get_dict(obj, function (data) {
          const code = data.code;

          resolve(data);
        });
      });
    },
    modifyPwd({ commit }, obj) {
      return new Promise((resolve, reject) => {
        modifyPwd(obj, function (data) {
          resolve(data);
        });
      });
    },

    getFaciliList({ commit }, obj) {
      return new Promise((resolve, reject) => {
        getFaciliListApi(obj, function (data) {
          resolve(data);
        });
      });
    },

    getFormDetail({ commit }, obj) {
      return new Promise((resolve, reject) => {
        getFormDetailApi(obj, function (data) {
          resolve(data);
        });
      });
    },

    getConclusionDetail({ commit }, obj) {
      return new Promise((resolve, reject) => {
        getConclusionDetailApi(obj, function (data) {
          resolve(data);
        });
      });
    },

    getProjectDetail({ commit }, obj) {
      return new Promise((resolve, reject) => {
        getProjectDetailApi(obj, function (data) {
          resolve(data);
        });
      });
    },
    putAuditProcess({ commit }, obj) {
      return new Promise((resolve, reject) => {
        putAuditProcessApi(obj, function (data) {
          resolve(data);
        });
      });
    },
  },
};

export default common;
